


clear all 
use the_dataset.dta

keep if  standard == 1


global covariates culture1 educ_basic1-educ_sup1 age051-age661 income1 NMIEMB1 size_dummy1-size_dummy5 number_spanish1 ccaadummy1-ccaadummy17

* change the following locals for different outcomes
* books1 books2 for books performance1 performance2 museums1 museums2
local out1 performance1
local out2 performance2

gen outcome1 = `out1'
gen outcome2 = `out2'
gen outcome_d = outcome2 - outcome1

* 1.1 Generate participation
gen m_m1 = 1 if outcome1 > 0
gen m_m2 = 1 if outcome2 > 0
replace m_m2 = 0 if outcome2 == 0


* 2.1 Estimation ATET on participation, T01
preserve
* SDID
absdid m_m2 if case_1==1, tvar(unemp) xvar($covariates yeardummy1-yeardummy9) sle 
eststo P1
*Matching
teffects nnmatch (m_m2 $covariates) (unemp) if case_1==1, ematch(ANOENC1) nneighbor(3) atet biasadj($covariates)
eststo P2
* Trimmed matching
gen var = _n
merge 1:1 var using "p_outcome_t01.dta"
drop _merge var

teffects nnmatch (m_m2 $covariates) (unemp) if case_1==1 &(outcome_1==1 | unemp==1),  ematch(ANOENC1) nneighbor(3) atet biasadj($covariates)
eststo P3
esttab P1 P2 P3  using mus_part1.tex, b(a5) se(2) star(* 0.1 ** 0.05 *** 0.01) ///
mtitle ("outcome Expenditure"  "Elasticity" "Elasticity") coeflabels ( r1vs0.unemp "outcome"  _cons "outcome" ) replace
restore

* 2.2 Estimation ATET on participation, T12
preserve
* SDid
absdid m_m2 if case_2==1, tvar(unemp) xvar($covariates yeardummy1-yeardummy9) sle 
eststo P1
*Matching
teffects nnmatch (m_m2 $covariates) (unemp) if case_2==1, ematch(ANOENC1) nneighbor(3) atet biasadj($covariates)
eststo P2
* Trimmed matching
gen var = _n
merge 1:1 var using "p_outcome_t12.dta"
drop _merge var

teffects nnmatch (m_m2 $covariates) (unemp) if case_2==1 &(outcome_2==1 | unemp==1),  ematch(ANOENC1) nneighbor(3) atet biasadj($covariates)
eststo P3

esttab P1 P2 P3  using mus_part2.tex, b(a5) se(2) star(* 0.1 ** 0.05 *** 0.01) ///
mtitle ("outcome Expenditure"  "Elasticity" "Elasticity") coeflabels ( r1vs0.unemp "outcome"  _cons "outcome" ) replace
restore

* 3.1 Estimation ATET on the outcome, T01
preserve
keep if case_1==1 & m_m2 == 1
* Sdid
absdid outcome_d, tvar(unemp) xvar($covariates yeardummy1-yeardummy9) sle 
matrix mus1_sp_m1 = e(b)
scalar mus1_sp_1 = mus1_sp_m1[1,1]
eststo C1
* Matching
teffects nnmatch (outcome_d $covariates) (unemp), ematch(ANOENC1) nneighbor(3) atet biasadj($covariates)
matrix mus1_mm_m1 = e(b)
scalar mus1_mm_1 = mus1_mm_m1[1,1]
eststo D1
* Trimmed matching
gen var = _n
merge 1:1 var using "m_outcome_t01.dta"
drop _merge var
keep  if  outcome_1==1 | unemp==1

teffects nnmatch (outcome_d $covariates) (unemp),  ematch(ANOENC1)  nneighbor(3) atet biasadj($covariates) 
matrix mus1_mt_m1 = e(b)
scalar mus1_mt_1 = mus1_mt_m1[1,1]
eststo E1
restore

*3.2 ATET on income T01
preserve
keep if case_1==1 & m_m2 == 1
absdid income_d, tvar(unemp) xvar($covariates yeardummy1-yeardummy9) sle 
matrix inc1_sp_m1 = e(b)
scalar inc1_sp_1 = inc1_sp_m1[1,1]
eststo C2

teffects nnmatch (income_d $covariates) (unemp) , ematch(ANOENC1) nneighbor(3) atet biasadj($covariates)
matrix inc1_mm_m1 = e(b)
scalar inc1_mm_1 = inc1_mm_m1[1,1]
eststo D2

* Trimmed matching
gen var = _n
merge 1:1 var using "m_outcome_t01.dta"
drop _merge var
keep if outcome_1==1 | unemp==1

teffects nnmatch (income_d $covariates ) (unemp), ematch(ANOENC1)  nneighbor(3) atet biasadj($covariates)
matrix inc1_mt_m1 = e(b)
scalar inc1_mt_1 = inc1_mt_m1[1,1]
eststo E2

esttab C1 C2  using mus_sp1.tex, b(a5) se(2) star(* 0.1 ** 0.05 *** 0.01) ///
mtitle ("outcome Expenditure"  "Elasticity" ) coeflabels ( r1vs0.unemp "outcome"  _cons "outcome" ) replace

esttab D1 D2  using mus_mm1.tex, b(a5) se(2) star(* 0.1 ** 0.05 *** 0.01) ///
mtitle ("outcome Expenditure"  "Elasticity" ) coeflabels ( r1vs0.unemp "outcome"  _cons "outcome" ) replace

esttab E1 E2  using mus_mt1.tex, b(a5) se(2) star(* 0.1 ** 0.05 *** 0.01) ///
mtitle ("outcome Expenditure"  "Elasticity" ) coeflabels ( r1vs0.unemp "outcome"  _cons "outcome" ) replace
restore

* 3.3 Estimation ATET on the outcome, T12
preserve
keep if case_2==1 & m_m2==1 
* Sdid
absdid outcome_d , tvar(unemp) xvar($covariates yeardummy1-yeardummy9) sle 
matrix mus2_sp_m1 = e(b)
scalar mus2_sp_1 = mus2_sp_m1[1,1]
eststo C3
* Matchinf
teffects nnmatch (outcome_d $covariates) (unemp), ematch(ANOENC1) nneighbor(3) atet biasadj($covariates)
matrix mus2_mm_m1 = e(b)
scalar mus2_mm_1 = mus2_mm_m1[1,1]
eststo D3
* Trimmed

gen var = _n
merge 1:1 var using "m_outcome_t12.dta"
drop _merge var
keep if case_2==1 &(outcome_2==1 | unemp==1) & m_m2==1
teffects nnmatch (outcome_d $covariates) (unemp), nneighbor(3) atet biasadj($covariates )
matrix mus2_mt_m1 = e(b)
scalar mus2_mt_1 = mus2_mt_m1[1,1]
eststo E3
restore

*3.4 ATET on income T12
preserve
keep if case_2==1 & m_m2==1
absdid income_d , tvar(unemp) xvar($covariates yeardummy1-yeardummy9) sle 
matrix inc2_sp_m1 = e(b)
scalar inc2_sp_1 = inc2_sp_m1[1,1]
eststo C4

teffects nnmatch (income_d $covariates) (unemp), ematch(ANOENC1) nneighbor(3) atet biasadj($covariates)
matrix inc2_mm_m1 = e(b)
scalar inc2_mm_1 = inc2_mm_m1[1,1]
eststo D4
* Trimmed matching

gen var = _n
merge 1:1 var using "m_outcome_t12.dta"
drop _merge var
keep if case_2==1 &(outcome_2==1 | unemp==1) & m_m2==1
teffects nnmatch (income_d $covariates yeardummy1-yeardummy9) (unemp), nneighbor(3) atet biasadj($covariates  yeardummy1-yeardummy9)
matrix inc2_mt_m1 = e(b)
scalar inc2_mt_1 = inc2_mt_m1[1,1]
eststo E4


esttab C3 C4  using mus_sp2.tex, b(a5) se(2) star(* 0.1 ** 0.05 *** 0.01) ///
mtitle ("outcome Expenditure"  "Elasticity" ) coeflabels ( r1vs0.unemp "outcome"  _cons "outcome" ) replace

esttab D3 D4  using mus_mm2.tex, b(a5) se(2) star(* 0.1 ** 0.05 *** 0.01) ///
mtitle ("outcome Expenditure"  "Elasticity" ) coeflabels ( r1vs0.unemp "outcome"  _cons "outcome" ) replace


esttab E3 E4  using mus_mt2.tex, b(a5) se(2) star(* 0.1 ** 0.05 *** 0.01) ///
mtitle ("outcome Expenditure"  "Elasticity" ) coeflabels ( r1vs0.unemp "outcome"  _cons "outcome" ) replace

restore

* 4 ELASTICITY
* gen a new vector
gen i1_sp_ = 0
gen i1_mm_ = 0
gen i1_mt_ = 0

gen m1_sp_ = 0
gen m1_mm_ = 0
gen m1_mt_ = 0

gen i2_sp_ = 0
gen i2_mm_ = 0
gen i2_mt_ = 0

gen m2_sp_ = 0
gen m2_mm_ = 0
gen m2_mt_ = 0

* replace it with estimations

replace i1_sp_ = inc1_sp_1
replace i1_mm_ = inc1_mm_1
replace i1_mt_ = inc1_mt_1

replace m1_sp_ = mus1_sp_1
replace m1_mm_ = mus1_mm_1
replace m1_mt_ = mus1_mt_1

replace i2_sp_ = inc2_sp_1
replace i2_mm_ = inc2_mm_1
replace i2_mt_ = inc2_mt_1

replace m2_sp_ = mus2_sp_1
replace m2_mm_ = mus2_mm_1
replace m2_mt_ = mus2_mt_1

* vector with average value 

sum outcome2 if case_1==1 & unemp==1 & m_m2==1
scalar m1 = r(mean)
sum outcome2 if case_2==1 & unemp==1 & m_m2==1
scalar m2 = r(mean)

sum income2 if case_1==1 & unemp==1 & m_m2==1
scalar i1 = r(mean)
sum income2 if case_2==1 & unemp==1 & m_m2==1
scalar i2 = r(mean)

gen m_1 = 0
gen m_2 = 0

gen i_1 = 0
gen i_2 = 0

replace m_1 = (2*m1)
replace m_2 = (2*m2)
replace i_1 = (2*i1)
replace i_2 = (2*i2)

* Variation
gen own_elast_sp_m1 = m1_sp_ / (m_1 - m1_sp_)
gen own_elast_mm_m1 = m1_mm_ / (m_1 - m1_mm_)
gen own_elast_mt_m1 = m1_mt_ / (m_1 - m1_mt_)

gen own_elast_sp_m2 = m2_sp_ / (m_2 - m2_sp_)
gen own_elast_mm_m2 = m2_mm_ / (m_2 - m2_mm_)
gen own_elast_mt_m2 = m2_mt_ / (m_2 - m2_mt_)

gen own_elast_sp_i1 = i1_sp_ / (i_1 - i1_sp_)
gen own_elast_mm_i1 = i1_mm_ / (i_1 - i1_mm_)
gen own_elast_mt_i1 = i1_mt_ / (i_1 - i1_mt_)

gen own_elast_sp_i2 = i2_sp_ / (i_2 - i2_sp_)
gen own_elast_mm_i2 = i2_mm_ / (i_2 - i2_mm_)
gen own_elast_mt_i2 = i2_mt_ / (i_2 - i2_mt_)

* Elasticity
gen elast_sp_m1 = own_elast_sp_m1 / own_elast_sp_i1
gen elast_mm_m1 = own_elast_mm_m1 / own_elast_mm_i1
gen elast_mt_m1 = own_elast_mt_m1 / own_elast_mt_i1

gen elast_sp_m2 = own_elast_sp_m2 / own_elast_sp_i2
gen elast_mm_m2 = own_elast_mm_m2 / own_elast_mm_i2
gen elast_mt_m2 = own_elast_mt_m2 / own_elast_mt_i2

sum elast_sp_m1 elast_mm_m1 elast_mt_m1
sum elast_sp_m2 elast_mm_m2 elast_mt_m2

*sample size
preserve
keep if case_1==1 & m_m2==1
*bys unemp: sum unemp

gen var = _n
merge 1:1 var using "m_outcome_t01.dta"
drop _merge var

keep if (outcome_1==1 | unemp==1)
bys unemp: sum unemp
restore

*sample size
preserve
keep if case_2==1 & m_m2==1
*bys unemp: sum unemp

gen var = _n
merge 1:1 var using "m_outcome_t12.dta"
drop _merge var

keep if (outcome_2==1 | unemp==1)
bys unemp: sum unemp
restore
